package com.cqupt.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqupt.mapper.SceneRecordMapper;
import com.cqupt.mapper.TimeRecordMapper;
import com.cqupt.mapper.UserWordMapper;
import com.cqupt.model.pojo.ArticleRecord;
import com.cqupt.model.pojo.SceneRecord;
import com.cqupt.model.pojo.TimeRecord;
import com.cqupt.model.pojo.UserWord;
import com.cqupt.model.vo.StudyDataVO;
import com.cqupt.service.ArticleRecordService;
import com.cqupt.mapper.ArticleRecordMapper;
import com.cqupt.utils.AuthContextUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Service
public class ArticleRecordServiceImpl extends ServiceImpl<ArticleRecordMapper, ArticleRecord>
        implements ArticleRecordService {


    @Resource
    private ArticleRecordMapper articleRecordMapper;
    @Resource
    private SceneRecordMapper sceneRecordMapper;
    @Resource
    private UserWordMapper userWordMapper;
    @Resource
    private TimeRecordMapper timeRecordMapper;


    @Override
    public StudyDataVO studyData() {
        Integer userId = AuthContextUtil.get().getId();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formatDate = sdf.format(date);
        LambdaQueryWrapper<UserWord> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(UserWord::getUserId, userId);
        List<UserWord> totalWord = userWordMapper.selectList(wrapper1);
        wrapper1.eq(UserWord::getDate, formatDate);
        List<UserWord> dayWord = userWordMapper.selectList(wrapper1);
        StudyDataVO studyDataVO = new StudyDataVO();
        if (ObjectUtil.isEmpty(dayWord)) {
            studyDataVO.setDayWordCount(0);
        } else studyDataVO.setDayWordCount(dayWord.size());
        if (ObjectUtil.isEmpty(totalWord)) {
            studyDataVO.setTotalWordCount(0);
        } else studyDataVO.setTotalWordCount(totalWord.size());

        LambdaQueryWrapper<ArticleRecord> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(ArticleRecord::getUserId, userId);
        List<ArticleRecord> totalArticle = articleRecordMapper.selectList(wrapper2);
        wrapper2.eq(ArticleRecord::getDate, formatDate);
        List<ArticleRecord> dayArticle = articleRecordMapper.selectList(wrapper2);
        if (ObjectUtil.isEmpty(dayArticle)) {
            studyDataVO.setDayArticleCount(0);
        } else studyDataVO.setDayArticleCount(dayArticle.size());
        if (ObjectUtil.isEmpty(totalArticle)) {
            studyDataVO.setTotalArticleCount(0);
        } else studyDataVO.setTotalArticleCount(totalArticle.size());


        LambdaQueryWrapper<SceneRecord> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(SceneRecord::getUserId, userId);
        List<SceneRecord> totalScene = sceneRecordMapper.selectList(wrapper3);
        wrapper3.eq(SceneRecord::getDate, formatDate);
        List<SceneRecord> dayScene = sceneRecordMapper.selectList(wrapper3);
        if (ObjectUtil.isEmpty(dayScene)) {
            studyDataVO.setDaySceneCount(0);
        } else studyDataVO.setDaySceneCount(dayScene.size());
        if (ObjectUtil.isEmpty(totalScene)) {
            studyDataVO.setTotalSceneCount(0);
        } else studyDataVO.setTotalSceneCount(totalScene.size());

        HashMap<String, Integer> map = new HashMap<>();
        LambdaQueryWrapper<TimeRecord> wrapper4 = new LambdaQueryWrapper<>();
        wrapper4.eq(TimeRecord::getUserId, userId);
        List<TimeRecord> timeRecords = timeRecordMapper.selectList(wrapper4);
        if (timeRecords.size() > 7) {
            timeRecords = timeRecords.stream().limit(7).toList();
        }
        for (TimeRecord timeRecord : timeRecords) {
            map.put(timeRecord.getDate(), timeRecord.getTime());
        }
        studyDataVO.setTimeRecord(map);

        return studyDataVO;
    }
}




